﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;

namespace StudentManagement.Tools
{
    /// <summary>
    /// 读取配置文件
    /// </summary>
    public class ConfigurationJson
    {
        /// <summary>
        /// 配置项
        /// </summary>
        private static readonly IConfigurationRoot Config;

        static ConfigurationJson()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");
            Config = builder.Build();
        }


        /// <summary>
        /// 获取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string this[string key]
        {
            get { return Config[key]; }
        }

        /// <summary>
        /// 获取配置文件中的Model
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetConfigModel<T>(string key) where T : class
        {
            return JsonSerializer.Deserialize<T>(Config[key], new JsonSerializerOptions() { Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), });
        }
    }
}
